package wli.hibernate;

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import wli.hibernate.util.HibernateUtil;
import wli.java.security.util.Md5Encryption;

public class UsersTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCreateUser() throws NoSuchAlgorithmException, UnsupportedEncodingException {
		System.out.println(">>>>>>>>>>>>>>AAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>>>>>>>>>>>>>");
		Set<Roles> role_set = new HashSet<Roles>();
		/*
		Roles usrl1 = new Roles();
		usrl1.setRoleName("admin");
		role_set.add(usrl1);

		Roles usrl2 = new Roles();

		usrl2.setRoleName("manager");
		role_set.add(usrl2);

		Roles usrl3 = new Roles();

		usrl3.setRoleName("manager-gui");
		role_set.add(usrl3);

		Roles usrl4 = new Roles();

		usrl4.setRoleName("manager-jmx");
		role_set.add(usrl4);
		//user.setUserRoles(role_set);

		Roles usrl5 = new Roles();

		usrl5.setRoleName("manager-status");
		role_set.add(usrl5);
*/
		
		Roles usrl6 = new Roles();
		usrl6.setRoleName("members");
		role_set.add(usrl6);
/*
		Roles usrl7 = new Roles();

		usrl7.setRoleName("tomcat");
		role_set.add(usrl7);
*/
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+role_set.size());
		Users user = new Users();
		user.setUserName("eli");
		user.setPassWord(Md5Encryption.MD5("li0578"));
		user.setUserRoles(role_set);

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			System.out.println("Test add user only....");
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();

			System.out.println("User [" + session.save(user) + "] Saved.");
			transaction.commit();
			session.close();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}